package com.cqut.hfh.ordering.service.Impl;

import com.cqut.hfh.ordering.pojo.*;
import com.cqut.hfh.ordering.service.IFoodService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.li.mybatisutil.dao.base.Dao;
import com.cqut.li.mybatisutil.dao.base.SearchDao;
import com.cqut.li.mybatisutil.entity.base.Entity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * create by  admin on 2019/6/25
 */
@Service
public class FoodServiceImpl implements IFoodService {
    @Resource
    private SearchDao searchDao;
    @Resource
    private Dao dao;

    @Override
    public Integer getCount() {
        return searchDao.sqlCount(Entity.getTableName(FodFood.class));
    }

    @Override
    public List<Map<String, Object>> getAllFood(String limit, String page) {
        String con = " 1 = 1 limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        String joinEntity = "left join fod_type on fod_food.type_id = fod_type.type_id " +
                "left join fod_merchant on fod_food.merchant_id=fod_merchant.merchant_id";
        List<Map<String, Object>> food_list
                = searchDao.searchForeign(new String[]{
                        "food_id",
                        "merchant_name",
                        "type_name",
                        "food_name",
                        "food_price",
                        "food_sell",
                        "fod_food.isva",
                        "fod_food.optime"},
                Entity.getTableName(FodFood.class),
                joinEntity,
                null,
                null,
                con);

        return food_list;
    }

    @Override
    public List<Map<String, Object>> getFoodByName(String limit, String page, String food_name) {
        String con = " food_name like '%" + food_name + "%' limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        String joinEntity = "left join fod_type on fod_food.type_id = fod_type.type_id " +
                "left join fod_merchant on fod_food.merchant_id=fod_merchant.merchant_id";
        List<Map<String, Object>> food_list
                = searchDao.searchForeign(new String[]{
                        "food_id",
                        "merchant_name",
                        "type_name",
                        "food_name",
                        "food_price",
                        "food_sell",
                        "fod_food.isva",
                        "fod_food.optime"},
                Entity.getTableName(FodFood.class),
                joinEntity,
                null,
                null,
                con);

        return food_list;
    }

    @Override
    public void AddFood(FodFood food) {
        Date date=new Date();
        food.setOptime(date);
        food.setFood_id(EntityIdutils.getId());
        dao.save(food);
    }

    @Override
    public Integer findMerchant(String merchant_id, String merchant_name) {
        String con = " merchant_id = '" + merchant_id + "'"
                +" and merchant_name = '" + merchant_name + "'";
        List<Map<String, Object>> foodMerchants
                = dao.findByCondition(new String[]{
                        "merchant_id",
                        "merchant_id",
                },
                con,
                FoodMerchant.class);

        if (foodMerchants != null && foodMerchants.size() != 0){
            return 0;
        }
        return 1;
    }

    @Override
    public void delById(String food_id) {
        dao.deleteByID(food_id, FodFood.class);
    }

    @Override
    public Map<String, Object> findById(String food_id) {
        Map<String, Object> map = dao.getByID(food_id, FodFood.class).toMap();
        List<FoodPic> byCondition = dao.getByCondition(" bus_id='" + food_id + "'", FoodPic.class);

        if ( byCondition != null && byCondition.size() != 0){
            map.put("pic_site", byCondition.get(0).getPic_site());
        }
        return map;
    }

    @Override
    public void updateFood(FodFood food,FoodMerchant foodMerchant) {
        Date date=new Date();
        food.setOptime(date);
        food.setMerchant_id(foodMerchant.getMerchant_id());
        dao.updatePropByID(food, food.getFood_id());
    }
}
